package com.ccntec.spark.core.tags.form.input;

import javax.servlet.jsp.JspException;

import com.ccntec.spark.core.BasicTagSupport;

public class SelectSimpleTag extends BasicTagSupport{
	
	private String name ;
	private String id ;
	private String data ;
	private String value;
	private String selectData;
	private String selectValue;
	private String split ;
	private boolean showDefault;
	private boolean disabled ;

	
	
	@Override
	public int doStartTag() throws JspException {
		/*
		 * <select class="form-control">
			<option value="Category 1">选项 1</option>
			<option value="Category 2">选项 2</option>
			<option value="Category 3">选项 3</option>
			<option value="Category 4">选项 4</option>
			</select>
		*/
		StringBuffer html = new StringBuffer();
		/*if(disabled){
			html.append("<fieldset disabled=\"\">");
		}
		 */		
		html.append("<div class=\"form-group\">");
		html.append("<select class=\"form-control\" name=\""+name+"\" ");
		if(!isNull(id)){
			html.append("id=\""+id+"\" ");
		}
		html.append(">");
		String[] dataArr = data.split(split);
		String[] valueArr = value.split(split);
		if(showDefault){
			html.append("<option value=\"\">请选择...</option>");
		}
		for (int i = 0; i < dataArr.length; i++) {
			//选项值优先
			if(!isNull(selectData)){
				if(selectData.equals(dataArr[i])){
					html.append("<option value=\""+valueArr[i]+"\" selected=\"selected\">"+dataArr[i]+"</option>");
				}else{
					html.append("<option value=\""+valueArr[i]+"\">"+dataArr[i]+"</option>");
				}
			}else if(!isNull(selectValue)){
				if(selectValue.equals(valueArr[i])){
					html.append("<option value=\""+valueArr[i]+"\" selected=\"selected\">"+dataArr[i]+"</option>");
				}else{
					html.append("<option value=\""+valueArr[i]+"\">"+dataArr[i]+"</option>");
				}
			}else{
				html.append("<option value=\""+valueArr[i]+"\">"+dataArr[i]+"</option>");
			}
		}
		html.append("</select>");
		outPrint(html);
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return EVAL_PAGE;
	}
	
	@Override
	public void release() {
		// TODO Auto-generated method stub
		super.release();
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setData(String data) {
		this.data = data;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public void setSplit(String split) {
		this.split = split;
	}
	public void setSelectData(String selectData) {
		this.selectData = selectData;
	}
	public void setSelectValue(String selectValue) {
		this.selectValue = selectValue;
	}
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}
	public void setShowDefault(boolean showDefault) {
		this.showDefault = showDefault;
	}
	
}
